package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Bank;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * BT_ITEM_ACCOUNT_DETAIL == > nd_responsibility_repayment
 *  项目台账管理( 担保责任还款记录表)
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO ND_RESPONSIBILITY_REPAYMENT(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, PRO_TYPE, REPAY_BANK, REPAYMENT_INTEREST_RATE, PLAN_REPAY, SHOULD_REPAYMENT_DATE, REPALY_MONEY, ACTUAL_REPAYMENT_DATE, CHECK_SIGN, REPAY_VOUCHER, VOUCHER_FILE_ID, ACCOUNT_REMARK, CHECK_TIME, LOAN_ID, SUBSIDIES, IS_VALID, PROCESS_ID, PRO_SOURCE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_RESPONSIBILITY_REPAYMENT SET CREATE_NAME = ?, CREATE_BY = ?, CREATE_DATE = ?, UPDATE_NAME = ?, UPDATE_BY = ?, UPDATE_DATE = ?, SYS_ORG_CODE = ?, SYS_COMPANY_CODE = ?, BPM_STATUS = ?, PRO_ID = ?, PRO_TYPE = ?, REPAY_BANK = ?, REPAYMENT_INTEREST_RATE = ?, PLAN_REPAY = ?, SHOULD_REPAYMENT_DATE = ?, REPALY_MONEY = ?, ACTUAL_REPAYMENT_DATE = ?, CHECK_SIGN = ?, REPAY_VOUCHER = ?, VOUCHER_FILE_ID = ?, ACCOUNT_REMARK = ?, CHECK_TIME = ?, LOAN_ID = ?, SUBSIDIES = ?, IS_VALID = ?, PROCESS_ID = ?, PRO_SOURCE = ? WHERE ID = ?",
        findMigratedIdListSql = "select ID from ND_RESPONSIBILITY_REPAYMENT where pro_id = ?",
        seq = 206
)
public class NdResponsibilityRepayment extends AbstractBusiness{
    public NdResponsibilityRepayment(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {

        for (Map btItemAccountDetail : this.sourceData.getBtItemAccountDetailList()) {
            if(!"2".equals(btItemAccountDetail.get("ACCOUNTTYPE").toString())){
                continue;
            }
            // 只迁移已核实记录
            if(!"2".equals(btItemAccountDetail.get("CHECKFLAG").toString())) {
                continue;
            }
            String id = buildId(btItemAccountDetail);
            // 判断是否需要覆盖迁移
            int needMigratedResult = this.needMigrated(id);
            if(needMigratedResult == 0) {
                continue;
            }

            // 单条插入或更新
            if(needMigratedResult == 1) {
            this.targetDbUtil.updateByParams(updateSql,this.buildValues(btItemAccountDetail),true);
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchUpdateValues.add(this.buildValues(btItemAccountDetail));
            } else {
            this.targetDbUtil.updateByParams(insertSql,this.buildValues(btItemAccountDetail));
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchInsertValues.add(this.buildValues(btItemAccountDetail));
            }
        }

    }

    public String buildId(Map btItemAccountDetail){
        // 基础表数据业务主键
        String originalId = btItemAccountDetail.get("ACCOUNTNO").toString();
        return originalId;
    }

    public List buildValues(Map btItemAccountDetail) throws Exception{
        List values = new ArrayList();

        //ID
        values.add(buildId(btItemAccountDetail));
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(Tool.toDate(btItemAccountDetail.get("CREATE_TIME")));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(Tool.toDate(btItemAccountDetail.get("MODIFY_TIME")));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add("1");

        //PRO_ID	项目编号
        values.add(this.projectId);
        //PRO_TYPE	账目类型
        values.add("2");
        //REPAY_BANK	还款银行
        values.add(DictTool.getTargetDictCode(Bank.class,btItemAccountDetail.get("BANKID")));
        //REPAYMENT_INTEREST_RATE	还款月利率 todo 有点坑 旧系统也是存储的月数据,单纯的年利率认为拆分成12个月，就是月利率
        values.add(btItemAccountDetail.get("YEARRATE"));
        //PLAN_REPAY	还款计划
        values.add(btItemAccountDetail.get("PLANFLAG"));
        //SHOULD_REPAYMENT_DATE	应还日期
        values.add(btItemAccountDetail.get("SHOULDTIME"));
        //REPALY_MONEY	还款额 | 万
        values.add(btItemAccountDetail.get("OCCURSUM"));
        //ACTUAL_REPAYMENT_DATE	实还日期
        values.add(btItemAccountDetail.get("OCCURTIME"));
        //CHECK_SIGN	核实标记
        values.add(btItemAccountDetail.get("CHECKFLAG") == null ? "0" :
                ("1".equals(btItemAccountDetail.get("CHECKFLAG").toString()) ? "0" : "1")
        );
        //REPAY_VOUCHER	还款凭证
        values.add(btItemAccountDetail.get("ELEFILENAME"));
        //VOUCHER_FILE_ID	凭证文件ID
        values.add(btItemAccountDetail.get("ELEFILESTORENAME") == null ?
                null : btItemAccountDetail.get("ELEFILESTORENAME").toString().replace("-",""));
        //ACCOUNT_REMARK	账目备注
        String accountRemark = btItemAccountDetail.get("ACCOUNTMEMO") == null ? "" : Tool.decodeUnicode(btItemAccountDetail.get("ACCOUNTMEMO").toString());
        accountRemark = accountRemark
            +"\n\n老系统其他字段信息"
            + "\n还款方式："+ BusinessTool.getOldDictDataNameByDictCodeAndDataCode("35",btItemAccountDetail.get("RETURNWAY"))
            + " \n还款计划："+ btItemAccountDetail.get("PLANFLAG") + "-" + BusinessTool.getOldDictDataNameByDictCodeAndDataCode("46",btItemAccountDetail.get("PLANFLAG"))
            + " \n还款月利率："+ btItemAccountDetail.get("MONTHRATE");

        values.add(accountRemark);
        //CHECK_TIME	复核时间
        values.add(btItemAccountDetail.get("CHECKTIME"));
        //LOAN_ID	放款编号
        values.add(btItemAccountDetail.get("LOANACCOUNTNO"));
        //SUBSIDIES	申请利息标记 todo
        values.add(null);
        //IS_VALID	是否使用
        values.add("Y");
        //PROCESS_ID	流程ID
        values.add(btItemAccountDetail.get("WORKFLOWNO") + "-JW");
        //PRO_SOURCE	项目来源
        values.add(null);




        return values;
    }

}
